const API_BASE_URL = 'https://www.gzyunck.com/app';

const request = (url, method, data) => {
  let _url = API_BASE_URL + '/' + url
  wx.showLoading({
    title: '正在加载中....',
  })
  return new Promise((resolve, reject) => {
    wx.request({
      url: _url,
      method: method,
      data: data,
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success(request) {

        if(request.data.rt == 204){
          // 未登录 那么就在后台再登录一下
          
        }
        wx.hideLoading();
        resolve(request.data)

      },
      fail(error) {
        reject(error)
      },
      complete(aaa) {
        // 加载完成
      }
    })
  })
}

/**
 * 小程序的promise没有finally方法，自己扩展下
 */
Promise.prototype.finally = function (callback) {
  var Promise = this.constructor;
  return this.then(
    function (value) {
      Promise.resolve(callback()).then(
        function () {
          return value;
        }
      );
    },
    function (reason) {
      Promise.resolve(callback()).then(
        function () {
          throw reason;
        }
      );
    }
  );
}
module.exports = {
  request,
  queryUsreInfo(key){
    return request('getUser', 'post', { sercet:key})
  },
  doLogin(){
    return new Promise((resolve,reject) => wx.login({
      success:resolve,
      fail:reject
    }))
  },
  backendLogin(code,info){
    return request('login', 'post', {
      openId: code,
      name: info.nickName,
      headImg: info.avatarUrl
    })
  },
  getNoticeList(key){
    return request('getNotices', 'get', { sercet: key })
  },
  getNoticeInfo(id,key){
    return request('getNotice','get',{sercet:key,id:id})
  },
  getRuleInfo(id,key){
    return request('getArticle', 'get', { sercet: key, id: id })
  },
  getArticles(key){
    return request('getArticles', 'get', { sercet: key })
  },
  getPointsInfo(key){
    return request('getIntergal', 'get', { sercet:key})
  },
  getFlow(key){
    return request('getFlow', 'get', { sercet: key })
  },
  getDepData(key){
    return request('getTypes', 'get', { sercet: key,type:3})
  },
  getTypeData(key){
    return request('getTypes', 'get', { sercet: key, type:2 })
  },
  submitPointsForm(data){
    return request('examine','post',data)
  },
  searchByWords(key,search){
    return request('search', 'get', { sercet: key, search: search})
  },
  getTypes(key,type){
    return request('getTypes', 'get', { sercet:key,type:type})
  },
  getOtherArticlesByType(key,type){
    return request('getOthers', 'get', { sercet:key,type:type})
  },
  getTypesArticles(key,types){
    let articlePromiseList = types.map(item=>{
      return this.getOtherArticlesByType(key,item.id);
    })
    return Promise.all(articlePromiseList);
  },
  getOtherArticlesByRules(key, type) {
    return request('getArticles', 'get', { sercet: key, type: type })
  },
  getTypesArticlesInRules(key,types){
    let articlePromiseList = types.map(item => {
      return this.getOtherArticlesByRules(key, item.id);
    })
    return Promise.all(articlePromiseList);
  },
  //获取评论列表
  getComment(key,id){
    return request('comments', 'get', { sercet: key, articleId: id }) 
  },
  addComment(key,id,comment){
    return request('addComment', 'post', { sercet: key, articleId: id,comment:comment})
  },
  setGood(key,id,isLike){
    return request('setGood', 'post', { sercet: key, articleId: id, isLike: isLike})
  },
  getNoticeByTag(key,id){
    return request('getNotiByTag', 'get', {sercet: key, tag: id })
  },
  getArtTag(key, id, atype){
    return request('getArtTag', 'get', { sercet: key, tag: id,atype:atype });
  },
  getQuery(key,keyword){
    return request('getQuery', 'get', { sercet: key, keyword:keyword})
  },
  submitPublishForm(key){
    return request('','post',data)
  }
}


      // {
      //   "pagePath": "pages/publish/publish",
      //   "iconPath": "static/image/publish.png",
      //   "selectedIconPath": "static/image/publish-selected.png",
      //   "text": "发布"
      // }, 配置文件配置。